package com.itcast.plane.modoule.person.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.plane.common.ConsHint;
import com.itcast.plane.modoule.indent.domain.Indent;
import com.itcast.plane.modoule.indent.mapper.IndentMapper;
import com.itcast.plane.modoule.person.domain.Person;
import com.itcast.plane.modoule.person.dto.PersonCreatDTO;
import com.itcast.plane.modoule.person.mapper.PersonMapper;
import com.itcast.plane.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PersonService extends ServiceImpl<PersonMapper, Person> {

   @Resource
   private PersonMapper personMapper;

   @Resource
   private IndentMapper indentMapper;

    public JsonResult addPerson(PersonCreatDTO creatDTO) {
        LambdaQueryWrapper<Person> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Person::getIdCard,creatDTO.getIdCard()).eq(Person::getUserId,creatDTO.getUserId());
        Person person = personMapper.selectOne(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(person)){
            Person person1 = new Person();
            BeanUtils.copyProperties(creatDTO,person1);
            personMapper.insert(person1);
            return new JsonResult(true, ConsHint.INSERT_SUCCESS);
        }else {
            return new JsonResult(false,ConsHint.PERSON_DEFAULT);
        }
    }

    public JsonResult deletePerson(int id) {
        LambdaQueryWrapper<Indent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Indent::getPersonId,id).in(Indent::getPay,1,0);
        int i = indentMapper.selectCount(lambdaQueryWrapper);
        if (i == 0) {
            personMapper.deleteById(id);
            return new JsonResult(true, ConsHint.DELETE_SUCCESS);
        }else {
            return new JsonResult(false,"该用户已有订单,不允许删除");
        }
    }

    public JsonResult editPerson(Person person) {
        LambdaQueryWrapper<Person> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Person::getIdCard,person.getIdCard()).eq(Person::getUserId,person.getUserId());
        Person person1 = personMapper.selectOne(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(person)){
            personMapper.updateById(person1);
            return new JsonResult(true,ConsHint.EDIT_SUCCESS);
        }else {
            return new JsonResult(false,ConsHint.PERSON_DEFAULT);
        }
    }

    public JsonResult getAll(int id) {
        LambdaQueryWrapper<Person> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Person::getUserId,id);
        List<Person> list = personMapper.selectList(lambdaQueryWrapper);
        return new JsonResult(true,list);
    }
}
